﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MetroCashLib;
using MetroCashLib.DataInfo;
using Windows.ApplicationModel.Resources;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace MetroCash
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class PProfileAccounts : MetroCash.Common.LayoutAwarePage
    {
        private ResourceLoader resLoader;
        private string profileName = String.Empty;
        public PProfileAccounts()
        {
            this.InitializeComponent();
            this.resLoader = new ResourceLoader();
            Loaded += PProfileAccounts_Loaded;
        }

        void PProfileAccounts_Loaded(object sender, RoutedEventArgs e)
        {

            this.pageTitle.Text = this.profileName;
            List<Account> commonAccount = MetroCashLib.DataBussiness.BAccount.getCommonAccounts(this.profileName);
            this.lstAccount.DataContext = commonAccount;
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            if (navigationParameter != null)
            {
                if (Windows.Storage.ApplicationData.Current.RoamingSettings.Values[SettingStrings.ROAMING_CURRENT_PROFILE] != null)
                {
                    this.profileName = Windows.Storage.ApplicationData.Current.RoamingSettings.Values[SettingStrings.ROAMING_CURRENT_PROFILE].ToString();
                }
                else
                {

                    this.profileName = (navigationParameter as String);
                }
                Windows.Storage.ApplicationDataContainer roamingSetting = Windows.Storage.ApplicationData.Current.RoamingSettings;
                //roamingSetting.Values[SettingStrings.ROAMING_ACCOUNT_BREADCRUMB] = new Stack<Account>();
                //roamingSetting.Values[SettingStrings.ROAMING_ACCOUNT_BREADCRUMB] = (new Account[1]).add;
            }
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        private void lstAccount_ItemClick_1(object sender, ItemClickEventArgs e)
        {
            Account selectedAccount = e.ClickedItem as Account;
            Windows.Storage.ApplicationDataContainer roamingSetting = Windows.Storage.ApplicationData.Current.RoamingSettings;
            if (roamingSetting.Values[SettingStrings.ROAMING_ACCOUNT_BREADCRUMB] != null)
            {
                //(roamingSetting.Values[SettingStrings.ROAMING_ACCOUNT_BREADCRUMB] as Stack<Account>).Push(selectedAccount);

            }
            this.Frame.Navigate(typeof(PAccountBrowse), selectedAccount);
        }

        async private void btnChangeProfileName_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                var profileFile = await StorageFile.GetFileFromPathAsync(Path.Combine(Windows.Storage.ApplicationData.Current.RoamingFolder.Path, SettingStrings.PROFILE_FOLDER_NAME, this.profileName));
                await profileFile.RenameAsync(this.txtNewProfileName.Text, NameCollisionOption.FailIfExists);
                var quitMsg = new MessageDialog(this.resLoader.GetString("Success-Body-ProfileName"), this.resLoader.GetString("Success-Header-ProfileName"));
                quitMsg.Commands.Add(new UICommand(this.resLoader.GetString("Success-Button-ProfileName-Change-OK")));
                await quitMsg.ShowAsync();
                this.popChangeProfileName.IsOpen = false;
                this.profileName = this.txtNewProfileName.Text;
                this.pageTitle.Text = this.profileName;
                Windows.Storage.ApplicationData.Current.RoamingSettings.Values[SettingStrings.ROAMING_CURRENT_PROFILE] = this.profileName;

            }
            catch (Exception ex)
            {
                var quitMsg = new MessageDialog(String.Format(this.resLoader.GetString("Error-Body-ProfileName-Duplicate"), profileName), this.resLoader.GetString("Error-Header-ProfileName-Duplicate"));
                quitMsg.Commands.Add(new UICommand(this.resLoader.GetString("Error-Button-ProfileName-Duplicate-OK")));
                quitMsg.ShowAsync();
                this.popChangeProfileName.IsOpen = true;
            }
        }

        private void btnAppBarEdit_Click_1(object sender, RoutedEventArgs e)
        {
            this.popChangeProfileName.IsOpen = true;
        }

        private void btnAppBarSwitch_Click_1(object sender, RoutedEventArgs e)
        {
            if (this.Frame != null)
            {
                this.Frame.Navigate(typeof(PProfileBrowse));
            }
        }

        private void btnAppBarAdd_Click_1(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(PCreateProfile));
        }
    }
}
